$(function () {
    $('#register').on('click', function () {
        $(this).parents('.login').hide().next('.register').show()
    })
    $('#login').on('click', function () {
        $(this).parents('.register').hide().prev('.login').show()
    })

    $('.register form').on('submit', function (e) {
        e.preventDefault();
        let data = $(this).serialize();
        
        $.ajax({
            type:'post',
            url:'http://www.itcbc.com:8080/api/reguser',
            data: data,
            cache:false,
            dataType:'json',
            success:function (res) {
                if(res.status === 0){
                    alert('注册成功');
                    $('.register form')[0].reset()
                }
            }
        })
        
    })

    let form = layui.form;
    form.verify({
        same: function (val) {
            let pwd = $('.pwd').val()
            if (pwd !== val) {
                return '两次密码不一致'
            }
        }
    })
    
     $('.login form').on('submit', function (e) {
        e.preventDefault();
        // console.log($(this).serialize());


        $.ajax({
            type: 'post',
            url: 'http://www.itcbc.com:8080/api/login',
            cache:false,
            processData:true,

            data: $(this).serialize(),
            success: function (res) {
                console.log(res);

                let token = JSON.stringify(res.token)
                console.log(res.token);
                if (res.status === 0) {
                    localStorage.setItem('token', res.token);
                    layer.msg(res.message, {
                        time: 2000
                    }, () => {
                        location.href = './nav/navigation.html';
                    });
                } else {
                    alert(res.message)
                }
            }
        })
    })
})